package cn.gcjxsc.repository.specs;

import cn.gcjxsc.common.consts.RedPackRecordRole;
import cn.gcjxsc.common.consts.RedPackType;
import cn.gcjxsc.common.data.query.RedPackRecordQuery;

import cn.gcjxsc.entity.merchant.redpack.RedPackRecord;
import cn.gcjxsc.entity.merchant.redpack.RedPackRecord_;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author pumbaa
 * @since 1.0
 */
public class RedPackSpec {

    public static Specification<RedPackRecord> buildSpec(RedPackRecordQuery filter) {
        return new Specification<RedPackRecord>() {
            @Override
            public Predicate toPredicate(Root<RedPackRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();

                if (filter.getStartTime() != null && filter.getStartTime() > 0) {
                    predicateList.add(cb.greaterThanOrEqualTo(root.get(RedPackRecord_.created), filter.getStartTime()));
                }

                if (filter.getEndTime() != null && filter.getEndTime() > 0) {
                    predicateList.add(cb.lt(root.get(RedPackRecord_.created), filter.getEndTime()));
                }

                //拥有人
                Integer ownerType = filter.getOwnerType();
                if (ownerType != null) {
                    if (ownerType == RedPackRecordRole.MEMBER.getType()) {
                        if (filter.getOwnerId() != null && filter.getOwnerId() > 0) {
                            predicateList.add(cb.equal(root.get(RedPackRecord_.targetId), filter.getOwnerId()));
                        }
                    } else if (ownerType == RedPackRecordRole.MERCHANT.getType()) {
                        if (filter.getOwnerId() != null && filter.getOwnerId() > 0) {
                            predicateList.add(cb.equal(root.get(RedPackRecord_.ownerId), filter.getOwnerId()));
                        }
                    }
                }
                Integer type = filter.getType();
                if (type != null && type > 0) {
                    if (type == 100) {
                        predicateList.add(root.get(RedPackRecord_.type).in(Arrays.asList(RedPackType.BUY.getType(), RedPackType.BUY_BACK.getType())));
                    }
                    else if (type == 200) {
                        predicateList.add(root.get(RedPackRecord_.type).in(Arrays.asList(RedPackType.SHARE.getType(), RedPackType.SHARE_BACK.getType())));
                    }
                    else {
                        predicateList.add(cb.equal(root.get(RedPackRecord_.type), type));
                    }
                }

                if (!StringUtils.isEmpty(filter.getSn())) {
                    predicateList.add(cb.like(root.get(RedPackRecord_.sourceSn), "%"+filter.getSn() + "%"));
                }
                if (!StringUtils.isEmpty(filter.getTradeNo())) {
                    predicateList.add(cb.equal(root.get(RedPackRecord_.tradeNo), filter.getTradeNo()));
                }
                if (!StringUtils.isEmpty(filter.getStatus())) {
                    predicateList.add(cb.equal(root.get(RedPackRecord_.status), filter.getStatus()));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }
}
